package com.ctshk.rpc.localfun.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ctshk.rpc.localfun.entity.LocalFunProduct;
import com.ctshk.rpc.localfun.entity.LocalFunProductSaleRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author wansi <gaylen15527491065@yeah.net>
 * @Date 2021/4/8
 */
public interface LocalFunProductSaleRecordMapper extends BaseMapper<LocalFunProductSaleRecord> {

    @Select("<script>"+"select case when sum(sold_number) is null then 0 else sum(sold_number) end  from t_local_fun_product_sale_record where product_id= #{product_id}" + "</script>")
    int selectSoldStockCount(@Param("product_id") Long productId);

    @Select("<script>"+"select case when sum(sold_number) is null then 0 else sum(sold_number) end  from t_local_fun_product_sale_record where product_id= #{product_id} and sales_date=#{now}" + "</script>")
    int selectTodaySoldStockCount(@Param("product_id") Long productId, @Param("now") Date now);

    @Select("<script>"+"select case when sum(sold_number) is null then 0 else sum(sold_number) end from t_local_fun_product_sale_record where product_id= #{product_id} and order_id=#{order_id}" + "</script>")
    int selectOrderSoldStockCount(@Param("product_id") Long productId,@Param("order_id") Long orderId);

    @Select("<script>"+"SELECT * FROM t_local_fun_product_sale_record WHERE date(sales_date) = curdate() and product_id=#{product_id} and channel=#{channel} <if test='checkType != null'> and check_type=#{checkType} </if> and line=#{line}" + "</script>")
    List<LocalFunProductSaleRecord> getCurrentSaleRecord(@Param("product_id") Long productId,@Param("channel")Integer channel,@Param("checkType") Integer checkType,@Param("line") Integer line);

    @Select("<script>"+"select case when sum(sold_number) is null then 0 else sum(sold_number) end from t_local_fun_product_sale_record where product_id= #{product_id} and channel=#{channelType} and check_type=#{checkType} and sales_date=#{now}" + "</script>")
    int queryDateSoldStockCount(@Param("product_id") Long productId, @Param("channelType") Integer channelType, @Param("checkType") Integer checkType, @Param("now") Date now);

    @Select("SELECT count(sold_number) from t_local_fun_product_sale_record where product_id = #{product_id}")
    int queryProductSaleCount(@Param("product_id") Long id);

    @Select("<script>"+"select case when sum(sold_number) is null then 0 else sum(sold_number) end  from t_local_fun_product_sale_record where product_id= #{productId} and sales_date=#{now} and custom_id=#{relationId} and line=#{line} and channel=#{channel}" + "</script>")
    int selectOnlineTodaySoldStockCount(@Param("productId") Long productId, @Param("now") Date now, @Param("relationId") Long relationId, @Param("line") int line, @Param("channel") int channel);
}
